/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package laboratory;

import ejb.laboratory.Controllo;
import ejb.laboratory.ControlloFacadeRemote;
import ejb.laboratory.ValidazioneFacadeRemote;
import ejb.warehouse.Articolo;
import ejb.warehouse.ArticoloFacadeRemote;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import system.Utilities;

import org.apache.struts.upload.FormFile;

import system.FileActionForm;
import warehouse.ArticleActionForm;
import warehouse.ProductActionForm;


/**
 *
 * @author ciummo
 */
public class CheckAction extends DispatchAction {


    private final static String SUCCESS = "success";
    private final static String SUCCESSCHECKLIST = "successCheckList";
    
    private final static String SUCCESSDELETECHECKLIST = "success";

    private final static String CHECKLISTDETAILS = "checkListDetails";

    Utilities utility = new Utilities();

    
    
    public ActionForward viewCheckList(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        String codArticolo = request.getParameter("codArticolo");
        
        ControlloFacadeRemote controlloFacade = (ControlloFacadeRemote) utility.callRemoteBean("ControlloFacade/remote");
        
        List <Controllo> controlli = (List) controlloFacade.findControlliBycodArticolo(codArticolo);
        
        ValidCheckActionForm checkListForm = new ValidCheckActionForm();
        
        checkListForm.setListCheck(controlli);
        
        
        /*
        SetControlliToForm setToForm = new SetControlliToForm();
        
        List<String> codArticoloList = new ArrayList();
        
        codArticoloList.add(codArticolo);*/
        
        request.getSession().setAttribute("codArticolo", codArticolo);
        request.getSession().setAttribute("ListaCheck", checkListForm);
        
        
        return mapping.findForward(CHECKLISTDETAILS);
    }
    
    public ActionForward listCheck(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        
        
            ControlloFacadeRemote controlloFacade = (ControlloFacadeRemote) utility.callRemoteBean("ControlloFacade/remote");          
                    
            ValidCheckActionForm checkForm = new ValidCheckActionForm();
                 
            
            checkForm.setListCheck(controlloFacade.findAll());
              

            
            List <String> codArticoliCheck = controlloFacade.findCodArticoloCheck();
            
            //request.getSession().setAttribute("CheckList", checkForm);
            
            SetControlliToForm setToForm = new SetControlliToForm();
            
            
            checkForm.setListaValidazioni(setToForm.setForm(codArticoliCheck));
            
            //List <ValidCheckActionForm> validCheckList = setToForm.setForm(codArticoliCheck);
            
            request.getSession().setAttribute("ListaValidazioni", checkForm);
            
            
            ArticoloFacadeRemote articoloFacade = (ArticoloFacadeRemote) utility.callRemoteBean("ArticoloFacade/remote");

            ArticleActionForm articoli = new ArticleActionForm();
        
            articoli.setList(articoloFacade.findArticoliNonCheck());
                       
            request.getSession().setAttribute("ListaArticoliNonCheck", articoli);              

            //articoli.setList(articoloFacade.findArticoliNonCheck());
                       
            //request.getSession().setAttribute("ListaArticoli", articoli);        
            
            
            
        return mapping.findForward(SUCCESSCHECKLIST);
    }

    public ActionForward deleteCheckList(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {    
    
    
        String codArticolo = request.getParameter("codArticolo");
        
        ControlloFacadeRemote controlloFacade = (ControlloFacadeRemote) utility.callRemoteBean("ControlloFacade/remote");
        
        controlloFacade.deleteControlli(codArticolo);
        
    
        return mapping.findForward(SUCCESSDELETECHECKLIST);
    }
    
    public ActionForward checkFromFile(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
                
            FileActionForm fileUploadForm = (FileActionForm)form;
 
	    FormFile file = fileUploadForm.getFile();

            ReadCheckFromFile readCheck = new ReadCheckFromFile();
            
            try{
            
            readCheck.Reader(file.getInputStream(), fileUploadForm.getProp());
            } catch (Exception e){
                e.printStackTrace();
            }
        return mapping.findForward(SUCCESS);
    }
}


class ReadCheckFromFile {

    private Utilities utility = new Utilities();
    
    void Reader (InputStream file, String codArticolo) throws FileNotFoundException, IOException, NamingException, ClassNotFoundException{
        
        
        InputStreamReader isr = new InputStreamReader(file);
        BufferedReader br = new BufferedReader(isr);
        
        String riga;

        ControlloFacadeRemote controlloFacade = (ControlloFacadeRemote) utility.callRemoteBean("ControlloFacade/remote");
        
        Controllo controllo;
        
        while((riga = br.readLine())!= null){
                 
            
            //TODO SE MODIFICO IL BEAN CONTROLLO MI CONVIENE PASSARE I PARAMETRI DAL COSTRUTTORE, MA SOLO QUANDO E' SICURO CHE NON LO CAMBIERO'
            
            
            controllo = new Controllo();
            
            controllo.setCodArticolo(codArticolo);
            controllo.setTesto(riga);
            
            controlloFacade.create(controllo);

            
        }
        
        
        
    }
       
    
}


class SetControlliToForm {
    
    private Utilities utility = new Utilities();
    
    List<ValidCheckActionForm> setForm (List<String> codArticoliCheck) throws NamingException{
        
        ControlloFacadeRemote controlloFacade = (ControlloFacadeRemote) utility.callRemoteBean("ControlloFacade/remote");
        
        ArticoloFacadeRemote articoloFacade = (ArticoloFacadeRemote) utility.callRemoteBean("ArticoloFacade/remote");
        
        List <ValidCheckActionForm> listCheckForm = new ArrayList();
        
        //ValidCheckActionForm checkForm = null;
        
        
        
        
        for (int i=0; i<codArticoliCheck.size(); i++){
                    
            ValidCheckActionForm checkForm = new ValidCheckActionForm();
            
            checkForm.setCodArticolo(codArticoliCheck.get(i));
            
            checkForm.setNomeProdotto(articoloFacade.find(codArticoliCheck.get(i)).getNome());
            
            
            List <Controllo> controlliArticolo = controlloFacade.findControlliBycodArticolo(codArticoliCheck.get(i));
            
            checkForm.setListCheck(controlliArticolo);
                
            listCheckForm.add(checkForm);
            
        }
        
        
        return listCheckForm;
        
        
    }
    
    
    
}